<?php 
global $_W,$_GPC;

$content = $this->message['content'];
include MODULE_ROOT.'/inc/mobile/common/global.func.php';

$qrcode = trim($_GPC['result']);
load()->model('mc');
if(strpos($qrcode,'imeepos_runner') == 0){
	$sql = "SELECT * FROM ".tablename('imeepos_runner3_tasks')." WHERE qrcode = :qrcode";
	$params = array(':qrcode'=>$qrcode);
	$tasks = pdo_fetch($sql,$params);
	if(!empty($tasks)){
		if($tasks['status'] == 1){
			json(0,'抱歉，此订单还没有人接哦，完成失败！');
		}
		if($tasks['status'] == 0){
			json(0,'抱歉，此订单尚未完成支付，完成失败！');
		}
		if($tasks['status'] == 4){
			json(0,'抱歉，此二维码已失效，完成失败！');
		}
		$sql = "SELECT * FROM ".tablename('imeepos_runner3_recive')." WHERE taskid = :taskid";
		$params = array(':taskid'=>$tasks['id']);
		$recive = pdo_fetch($sql,$params);
		if(empty($recive)){
			json(0,'接单人信息错误，完成失败！');
		}
		if($_W['openid'] != $recive['openid']){
			json(0,'权限错误，这不是您的单子，请确认！');
		}
		pdo_update('imeepos_runner3_tasks',array('status'=>4),array('id'=>$tasks['id']));
		if($tasks['type'] == 3 || $task['type'] == 2){
			//帮我买
			$sql = "SELECT * FROM ".tablename('imeepos_runner3_buy')." WHERE taskid = :taskid";
			$params = array(':taskid'=>$tasks['id']);
			$detail = pdo_fetch($sql,$params);
			$fee = $detail['freight'];
		}else{
			$sql = "SELECT * FROM ".tablename('imeepos_runner3_detail')." WHERE taskid = :taskid";
			$params = array(':taskid'=>$tasks['id']);
			$detail = pdo_fetch($sql,$params);
			$fee = $detail['total'];
		}
		
		$uid = mc_openid2uid($recive['openid']);
		mc_credit_update($uid, 'credit2',$fee,array($uid, '跑腿佣金', 0, 0));
		//插入记录表
		$data = array();
		$data['uniacid'] = $_W['uniacid'];
		$data['openid'] = $_W['openid'];
		$data['create_time'] = time();
		$data['reciveid'] = $recive['id'];
		$data['fee'] = $fee;

		$sql = "SELECT * FROM ".tablename('imeepos_runner3_moneylog')." WHERE reciveid = :reciveid";
		$params = array(':reciveid'=>$recive['id']);
		$m = pdo_fetch($sql,$params);

		if(!empty($m)){
			//赏金到账通知
			$content = "";
			$content = "恭喜您，".$fee."赏金已到账！~\n";
			$content .= "订单编号：".$tid."\n";
			$content .= "时间：".date('Y年m月d日 h点i分')."\n";
			$content .= "咚咚咚，恭喜您，恭喜您，任务完成!赏金".$fee."元已到账余额，请注意查收~，点击继续赚钱";
			$url = $_W['siteroot'].'app/'.$this->createMobileUrl('index');
			$retrun = mc_notice_consume2($_W['openid'], '赏金到账通知', $content, $url,'');
				
			json(1,'恭喜您，任务完成!赏金'.$fee.'元已到账余额，请注意查收');
		}else{
			$content = "";
			$content = "恭喜您，".$fee."赏金已到账！~\n";
			$content .= "订单编号：".$tid."\n";
			$content .= "时间：".date('Y年m月d日 h点i分')."\n";
			$content .= "咚咚咚，恭喜您，恭喜您，任务完成!赏金".$fee."元已到账余额，请注意查收~，点击继续赚钱";
			$url = $_W['siteroot'].'app/'.$this->createMobileUrl('index');
			$retrun = mc_notice_consume2($_W['openid'], '赏金到账通知', $content, $url,'');
				
			pdo_insert('imeepos_runner3_moneylog',$data);
			json(1,'恭喜您，任务完成!赏金'.$fee.'元已到账余额，请注意查收');
		}
	}else{
		json(0,'抱歉，任务完成失败，请确认！');
	}
}
?>